/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.core.form.web.FormAction
   Module Description   :

   Date Created      : 2011/4/26
   Original Author   : cut7man
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.core.form.web;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.form.entity.Attribute;
import com.yaodian100.core.form.entity.Form;

/**
 * @author cut7man
 * 
 */
public class FormAction {
	private Object[] forms;
	private Form theForm;
	private Object[] attributes;
	private String formName;
	private String formDescription;
	private String attributeId;
	@Resource
	private BaseManager baseMgr;

	private String[] id;
	private String[] name;
	private String[] dataType;
	private String[] mandatory;
	private String[] maxLength;
	private String[] minLength;
	private String[] maxValue;
	private String[] minValue;
	private String[] tip;
	private String[] description;
	private String[] defaultValue;
	private String[] presentValues;
	private String[] dateFormat;
	private String[] presentNames;
	private String[] componentUrl;
	private String[] sortOrder;
	private String message;

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the formDescription
	 */
	public String getFormDescription() {
		return formDescription;
	}

	/**
	 * @param formDescription the formDescription to set
	 */
	public void setFormDescription(String formDescription) {
		this.formDescription = formDescription;
	}

	/**
	 * @return the name
	 */
	public String[] getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String[] name) {
		this.name = name;
	}

	/**
	 * @return the dataType
	 */
	public String[] getDataType() {
		return dataType;
	}

	/**
	 * @param dataType the dataType to set
	 */
	public void setDataType(String[] dataType) {
		this.dataType = dataType;
	}

	/**
	 * @return the mandatory
	 */
	public String[] getMandatory() {
		return mandatory;
	}

	/**
	 * @param mandatory the mandatory to set
	 */
	public void setMandatory(String[] mandatory) {
		this.mandatory = mandatory;
	}

	/**
	 * @return the maxLength
	 */
	public String[] getMaxLength() {
		return maxLength;
	}

	/**
	 * @param maxLength the maxLength to set
	 */
	public void setMaxLength(String[] maxLength) {
		this.maxLength = maxLength;
	}

	/**
	 * @return the minLength
	 */
	public String[] getMinLength() {
		return minLength;
	}

	/**
	 * @param minLength the minLength to set
	 */
	public void setMinLength(String[] minLength) {
		this.minLength = minLength;
	}

	/**
	 * @return the maxValue
	 */
	public String[] getMaxValue() {
		return maxValue;
	}

	/**
	 * @param maxValue the maxValue to set
	 */
	public void setMaxValue(String[] maxValue) {
		this.maxValue = maxValue;
	}

	/**
	 * @return the minValue
	 */
	public String[] getMinValue() {
		return minValue;
	}

	/**
	 * @param minValue the minValue to set
	 */
	public void setMinValue(String[] minValue) {
		this.minValue = minValue;
	}

	/**
	 * @return the tip
	 */
	public String[] getTip() {
		return tip;
	}

	/**
	 * @param tip the tip to set
	 */
	public void setTip(String[] tip) {
		this.tip = tip;
	}

	/**
	 * @return the description
	 */
	public String[] getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String[] description) {
		this.description = description;
	}

	/**
	 * @return the defaultValue
	 */
	public String[] getDefaultValue() {
		return defaultValue;
	}

	/**
	 * @param defaultValue the defaultValue to set
	 */
	public void setDefaultValue(String[] defaultValue) {
		this.defaultValue = defaultValue;
	}

	/**
	 * @return the presentValues
	 */
	public String[] getPresentValues() {
		return presentValues;
	}

	/**
	 * @param presentValues the presentValues to set
	 */
	public void setPresentValues(String[] presentValues) {
		this.presentValues = presentValues;
	}

	/**
	 * @return the dateFormat
	 */
	public String[] getDateFormat() {
		return dateFormat;
	}

	/**
	 * @param dateFormat the dateFormat to set
	 */
	public void setDateFormat(String[] dateFormat) {
		this.dateFormat = dateFormat;
	}

	/**
	 * @return the presentNames
	 */
	public String[] getPresentNames() {
		return presentNames;
	}

	/**
	 * @param presentNames the presentNames to set
	 */
	public void setPresentNames(String[] presentNames) {
		this.presentNames = presentNames;
	}

	/**
	 * @return the componentUrl
	 */
	public String[] getComponentUrl() {
		return componentUrl;
	}

	/**
	 * @param componentUrl the componentUrl to set
	 */
	public void setComponentUrl(String[] componentUrl) {
		this.componentUrl = componentUrl;
	}

	/**
	 * @return the sortOrder
	 */
	public String[] getSortOrder() {
		return sortOrder;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String[] id) {
		this.id = id;
	}

	/**
	 * @return the baseMgr
	 */
	public BaseManager getBaseMgr() {
		return baseMgr;
	}

	/**
	 * @param baseMgr the baseMgr to set
	 */
	public void setBaseMgr(BaseManager baseMgr) {
		this.baseMgr = baseMgr;
	}

	/**
	 * @return the id
	 */
	public Object[] getId() {
		return id;
	}

	/**
	 * @param sortOrder the sortOrder to set
	 */
	public void setSortOrder(String[] sortOrder) {
		this.sortOrder = sortOrder;
	}

	/**
	 * @return the theForm
	 */
	public Form getTheForm() {
		return theForm;
	}

	/**
	 * @param theForm the theForm to set
	 */
	public void setTheForm(Form theForm) {
		this.theForm = theForm;
	}

	/**
	 * @return the formName
	 */
	public String getFormName() {
		return formName;
	}

	/**
	 * @param formName the formName to set
	 */
	public void setFormName(String formName) {
		this.formName = formName;
	}

	/**
	 * @return the attributeId
	 */
	public String getAttributeId() {
		return attributeId;
	}

	/**
	 * @param attributeId the attributeId to set
	 */
	public void setAttributeId(String attributeId) {
		this.attributeId = attributeId;
	}

	/**
	 * @return the forms
	 */
	public Object[] getForms() {
		return forms;
	}

	/**
	 * @param forms the forms to set
	 */
	public void setForms(Object[] forms) {
		this.forms = forms;
	}

	/**
	 * @return the attributes
	 */
	public Object[] getAttributes() {
		return attributes;
	}

	/**
	 * @param attributes the attributes to set
	 */
	public void setAttributes(Object[] attributes) {
		this.attributes = attributes;
	}

	public String list() {
		forms = baseMgr.queryByHql("from Form order by name asc", null);
		return "list";
	}

	public String getForm() {
		forms = baseMgr.queryByHql("from Form order by name asc", null);
		if (forms != null) {
			for (Object obj : forms) {
				Form form = (Form) obj;
				if (form.getName().equals(formName)) {
					theForm = form;
					formName = theForm.getName();
					formDescription = theForm.getDescription();
					if (theForm.getAttributes() != null) {
						id = new String[theForm.getAttributes().size()];
						name = new String[theForm.getAttributes().size()];
						dataType = new String[theForm.getAttributes().size()];
						mandatory = new String[theForm.getAttributes().size()];
						maxLength = new String[theForm.getAttributes().size()];
						minLength = new String[theForm.getAttributes().size()];
						maxValue = new String[theForm.getAttributes().size()];
						minValue = new String[theForm.getAttributes().size()];
						tip = new String[theForm.getAttributes().size()];
						description = new String[theForm.getAttributes().size()];
						defaultValue = new String[theForm.getAttributes().size()];
						presentValues = new String[theForm.getAttributes().size()];
						dateFormat = new String[theForm.getAttributes().size()];
						presentNames = new String[theForm.getAttributes().size()];
						componentUrl = new String[theForm.getAttributes().size()];
						sortOrder = new String[theForm.getAttributes().size()];
						setAttributeObject(theForm);
					}
				}
			}
		}
		return "getForm";
	}

	private void setAttributeObject(Form theForm) {

		for (int j = 0; j < theForm.getAttributes().size(); j++) {
			id[j] = theForm.getAttributes().get(j).getId();
			name[j] = theForm.getAttributes().get(j).getName();
			dataType[j] = theForm.getAttributes().get(j).getDataType();
			mandatory[j] = String.valueOf(theForm.getAttributes().get(j).getMandatory());
			maxLength[j] = String.valueOf(theForm.getAttributes().get(j).getMaxLength());
			minLength[j] = String.valueOf(theForm.getAttributes().get(j).getMinLength());
			maxValue[j] = String.valueOf(theForm.getAttributes().get(j).getMaxValue());
			minValue[j] = String.valueOf(theForm.getAttributes().get(j).getMinValue());
			tip[j] = theForm.getAttributes().get(j).getTip();
			description[j] = theForm.getAttributes().get(j).getDescription();
			defaultValue[j] = theForm.getAttributes().get(j).getDefaultValue();
			presentValues[j] = theForm.getAttributes().get(j).getPresentValues();
			dateFormat[j] = theForm.getAttributes().get(j).getDateFormat();
			presentNames[j] = theForm.getAttributes().get(j).getPresentNames();
			componentUrl[j] = theForm.getAttributes().get(j).getComponentUrl();
			sortOrder[j] = String.valueOf(theForm.getAttributes().get(j).getSortOrder());
		}
	}

	public String update() {
		Form form = (Form) baseMgr.get(Form.class, formName);
		boolean newForm = false;
		if (form == null) {
			form = new Form();
			newForm = true;
		}
		form.setName(formName);
		form.setDescription(formDescription);
		if (newForm) {
			baseMgr.save(form);
		} else {
			baseMgr.update(form);
		}
		List<Attribute> attrs = new ArrayList<Attribute>();
		Object[] attributes = baseMgr.queryByHql("from Attribute where form.name=?", new Object[] { formName });
		if (attributes != null) {
			for (Object obj : attributes) {
				boolean isExist = false;
				Attribute attr = (Attribute) obj;
				for (String theId : id) {
					if (attr.getId().equals(theId)) {
						isExist = true;
						break;
					}
				}
				if (!isExist) {
					baseMgr.delete(attr);
				}
			}
		}
		if (id != null && id.length > 0) {
			for (int i = 0; i < id.length; i++) {
				Attribute attr = null;
				if (id[i] == null || id[i].length() == 0) {
					attr = new Attribute();
				} else {
					attr = (Attribute) baseMgr.get(Attribute.class, id[i]);
				}
				attr.setComponentUrl(componentUrl[i]);
				attr.setDataType(dataType[i]);
				attr.setDateFormat(dateFormat[i]);
				attr.setDefaultValue(defaultValue[i]);
				attr.setDescription(description[i]);
				attr.setForm(form);
				if (mandatory[i] != null && mandatory[i].length() > 0) {
					attr.setMandatory(Boolean.valueOf(mandatory[i]));
				}
				attr.setMaxLength(Integer.valueOf(maxLength[i]));
				attr.setMinLength(Integer.valueOf(minLength[i]));
				attr.setMaxValue(Integer.valueOf(maxValue[i]));
				attr.setMinValue(Integer.valueOf(minValue[i]));
				attr.setName(name[i]);
				attr.setPresentNames(presentNames[i]);
				attr.setPresentValues(presentValues[i]);
				if (sortOrder[i] != null && sortOrder[i].length() > 0) {
					attr.setSortOrder(Integer.valueOf(sortOrder[i]));
				}
				attr.setTip(tip[i]);
				baseMgr.saveOrUpdate(attr);
				attrs.add(attr);
			}
		}
		form.setAttributes(attrs);
		theForm = form;
		setAttributeObject(theForm);
		list();
		message = "表单已储存";
		return "update";
	}

	public String delete() {

		return "delete";
	}

	public String addColumn() {
		list();
		method = "addColumn";
		return "addColumn";
	}

	private String method;

	/**
	 * @return the method
	 */
	public String getMethod() {
		return method;
	}

	/**
	 * @param method the method to set
	 */
	public void setMethod(String method) {
		this.method = method;
	}
}
